package com.java.ratelimter.common;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 公共返回类
 */
public class CommonResult<T> {

    private String code;

    private String msg;

    private Integer count;

    private List<T> data;

    private Boolean status;

    public Integer getCount() {
        if (CollectionUtils.isEmpty(data)) {
            this.setData(new ArrayList<>());
        }
        return this.count = this.getData().size();
    }


    public CommonResult() {
        this.code = "0";
        this.msg = "成功";
    }

    public CommonResult(List<T> data) {
        this.code = "0";
        this.msg = "成功";
        this.data = data;
    }

    public CommonResult(String code, String msg,Boolean status) {
        this.code = code;
        this.msg = msg;
        this.status = status;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CommonResult<?> that = (CommonResult<?>) o;
        return Objects.equals(code, that.code) &&
                Objects.equals(msg, that.msg) &&
                Objects.equals(count, that.count) &&
                Objects.equals(data, that.data);
    }

    @Override
    public int hashCode() {
        return Objects.hash(code, msg, count, data);
    }

    public static void main(String[] args) {
        CommonResult commonResult = new CommonResult();
        commonResult.setCode("ddd");

        CommonResult commonResult2 = new CommonResult();
        commonResult2.setCode("ddd");

        System.out.println(commonResult.equals(commonResult2));




    }
}
